package com.haorui.lygyd.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.nio.charset.Charset;

/**
 * @author 陈刚
 * @version 1.0
 * @project ZNSM
 * @package com.haorui.config
 * @createDate 2017-08-09 14:21
 * @modfiyDate
 * @function
 */
@Configuration
@Slf4j
public class CorsConfig extends WebMvcConfigurerAdapter {
    @Value("${cors.origins}")
    private String origins;
    @Value("${server.http.port}")
    private Integer serverHttpPort;
    @Value("${server.port}")
    private Integer serverPort;
    //    @Value("${server.https.keystore}")
//    private String keystoreFile;
//    @Value("${server.https.keystorePassword}")
//    private String keystorePassword;
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        String[] originsArray=null;
        if(origins==null){
            originsArray=new String[]{"http://127.0.0.1"};
        }else{
            originsArray=origins.split(",");
        }
        registry.addMapping("/**")
                .allowedOrigins(originsArray)
                .allowedHeaders("*")
                .allowCredentials(true)
                .allowedMethods("*")
                .maxAge(3600);
    }

    //这东西有问题
    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };

        tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
        return tomcat;
    }

    private Connector initiateHttpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
        protocol.setSSLProtocol("TLSv1+TLSv1.1+TLSv1.2");
        connector.setScheme("http");
        //需要重定向的http端口
        connector.setPort(serverHttpPort);
        connector.setSecure(false);
        //设置重定向到https端口
        connector.setRedirectPort(serverPort);
        return connector;
    }
}
